local description = [[
# 混战模式（精简版）

超简洁规则：

- 出牌阶段空闲时间点，所有角色都能如在自己的出牌阶段般使用牌或发动技能，手速最快的先结算。
- 胜利条件为击杀所有其他角色，成为唯一的生存者。击杀后摸2。

]]

local simple_melee_getLogic = function()
  local simple_melee_logic = GameLogic:subclass("simple_melee_logic")

  function simple_melee_logic:assignRoles()
    local room = self.room
    local n = #room.players

    for i = 1, n do
      local p = room.players[i]
      p.role = "hidden"
      room:setPlayerProperty(p, "role_shown", true)
      room:broadcastProperty(p, "role")
      --p.role = p._splayer:getScreenName() --结算显示更好，但身份图标疯狂报错
    end

    self.start_role = "hidden"
    -- for adjustSeats
    room.players[1].role = "lord"
  end

  function simple_melee_logic:chooseGenerals()
    local room = self.room
    local generalNum = room.settings.generalNum
    local n = room.settings.enableDeputy and 2 or 1
    local lord = room:getLord()
    room:setCurrent(lord)
    lord.role = self.start_role

    local players = room.players
    local generals = room:getNGenerals(#players * generalNum)
    local req = Request:new(players, "AskForGeneral")
    table.shuffle(generals)
    for i, p in ipairs(players) do
      local arg = table.slice(generals, (i - 1) * generalNum + 1, i * generalNum + 1)
      req:setData(p, { arg, n })
      req:setDefaultReply(p, table.random(arg, n))
    end

    -- room:doBroadcastNotify("ShowToast", Fk:translate("chaos_intro"))

    req:ask()

    local selected = {}
    for _, p in ipairs(players) do
      local gs = req:getResult(p)
      local general = gs[1]
      local deputy = gs[2]
      room:setPlayerGeneral(p, general, true, true)
      room:setDeputyGeneral(p, deputy)
    end
    generals = table.filter(generals, function(g) return not table.contains(selected, g) end)
    room:returnToGeneralPile(generals)

    room:askForChooseKingdom(players)
  end

  return simple_melee_logic
end

local simple_melee_rule = fk.CreateTriggerSkill{
  name = "#simple_melee_rule",
  priority = 0.001,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Play
  end,
  on_trigger = function(_, _, _, player, _)
    local room = player.room
    local logic = room.logic
    if player._phase_end then return end
    room:doBroadcastNotify("UpdateSkill", "", {player})
    while not player.dead do
      if player._phase_end then break end
      local data = { timeout = room.timeout }
      logic:trigger(fk.StartPlayCard, player, data, true)

      local req = Request:new(room.alive_players, "PlayCard", 1)
      req.timeout = data.timeout
      req:ask()

      local p = req.winners[1]
      if not p then break end
      local result = req:getResult(p)
      if result == "" then break end

      local useResult = room:handleUseCardReply(p, result)
      if type(useResult) == "table" then
        room:useCard(useResult)
      end
    end
    return true
  end
}
Fk:addSkill(simple_melee_rule)
local simple_melee_mode = fk.CreateGameMode{
  name = "simple_melee_mode",
  minPlayer = 2,
  maxPlayer = 8,
  rule = simple_melee_rule,
  logic = simple_melee_getLogic,
  surrender_func = function(self, playedTime)
    return {
      {
        text = "chaos: left two alive",
        passed = #table.filter(Fk:currentRoom().players, function(p)
          return p.rest > 0 or not p.dead
        end) == 2
      }
    }
  end,
  reward_punish = function (self, victim, killer)
    if not killer or killer.dead then return end
    killer:drawCards(2, "kill")
  end,
  winner_getter = function(self, victim)
    if not victim.surrendered and victim.rest > 0 then
      return ""
    end
    local room = victim.room
    local alive = table.filter(room.players, function(p)
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)
    if #alive > 1 then return "" end
    alive[1].role = "renegade" --生草
    if room.class.name == 'Room' then
      room:broadcastProperty(alive[1], 'role')
    end
    return "renegade"
  end,
}

Fk:loadTranslationTable{
  ["simple_melee_mode"] = "混战模式（精简版）",
  [":simple_melee_mode"] = description,
}

return simple_melee_mode
